const _stat = module.exports

_stat.noticeDetailCount = async (args = {}) => {
    return M('notice_task_detail').count({ ...args })
}

_stat.noticeErrorDetail = async (args = {}) => {
    let org_id = args.org_id || ''
    if (!org_id) throw new Error('org_id is required')

    let _last = await dbUtil.getList('notice_task', {
        org_id: org_id,
        'way.way_code': {
            $in: [
                'PROVIDER_ALI_CLOUD_SMS',
                'PROVIDER_ALI_CLOUD_MNS'
            ]
        }
    }, 1, 10, '-created_at', [{
        model: 'notice_task_detail',
        foreignKey: 'id',
        targetKey: 'notice_task_id',
        relation: 'many',
        as: 'detail_info'
    }])

    let err_info = [];
    let details = _last.rows.map(m=>m['detail_info']||[]).reduce((acc,cur)=>acc.concat(cur),[]);
    for(let i=0;i<details.length; i++) {
        let _item = details[i];
        if(_item.send_status.code === 2) break
        else if(_item.send_status.code === 3) {
            err_info.push(_item.send_status.message.split(',')[0])
        }
    }

    return _.uniq(err_info)
}
